package com.ewanghuiju.app.ui.my.activity;

import android.view.Gravity;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.BaseActivity;
import com.ewanghuiju.app.base.contract.pintuan.ShopAddressContract;
import com.ewanghuiju.app.component.RxBus;
import com.ewanghuiju.app.model.bean.response.AddressListResponBean;
import com.ewanghuiju.app.model.event.SendEvent;
import com.ewanghuiju.app.presenter.my.ShopAddressPresenter;
import com.ewanghuiju.app.ui.my.adapter.AddressListAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.ewanghuiju.app.util.nodoubleclick.AntiShake;
import com.ewanghuiju.app.widget.popupwindow.CustomBasePopup;
import com.gyf.immersionbar.ImmersionBar;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author sj  qq:751943509
 * @class 收货地址列表页面
 * @time 2020/2/24
 */
public class ShopAddressListActivity extends BaseActivity<ShopAddressPresenter> implements ShopAddressContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private AddressListAdapter adapter;

    private int mType;

    @Override
    protected int getLayout() {
        return R.layout.activity_shop_address_list;
    }

    @Override
    protected void initEventAndData() {
        mType = getIntent().getIntExtra(Constants.TO_ADDRESS_LIST_TYPE, -1);
        initAddressList();
    }

    private void initAddressList() {

        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        linearLayoutManager.setSmoothScrollbarEnabled(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new AddressListAdapter(R.layout.item_address_list);
        adapter.setHasStableIds(true);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mType == 2) {
                    RxBus.getDefault().post(new SendEvent("", Constants.TYPE_ADDRESS_SELECTED, adapter.getItem(position)));
                    onBackPressedSupport();
                } else {
                    new StartActivityUtil(mContext, AddAddressActivity.class).putExtra(Constants.ADDRESS_INFO, adapter.getItem(position))
                            .startActivity(true);
                }
            }
        });
        adapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                CustomBasePopup customBasePopup = new CustomBasePopup(mContext, "是否删除当前地址？", "", "",
                        new CustomBasePopup.PopupWindowCallback() {
                            @Override
                            public void sureClickCallback() {
                                try {
                                    LoadingDialogUtils.show(mContext);
                                    AddressListResponBean addressListResponBean = (AddressListResponBean) adapter.getItem(position);
                                    mPresenter.deleteAddress(addressListResponBean.getId());
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void cancelClickCallback() {

                            }
                        });
                customBasePopup.setPopupGravity(Gravity.CENTER);
                customBasePopup.showPopupWindow();
                return false;
            }
        });
        recyclerView.setAdapter(adapter);
        LoadingDialogUtils.show(mContext);
        mPresenter.getData();
    }

    @OnClick({R.id.tv_create_address})
    public void doClick(View view) {
        if (AntiShake.check(view.getId())) {    //判断是否多次点击
            return;
        }
        switch (view.getId()) {
            case R.id.tv_create_address:
                //提交
                new StartActivityUtil(mContext, AddAddressActivity.class)
                        .startActivity(true);
                break;
            default:
                break;
        }
    }

    @Override
    protected int getTitleIntText() {
        return R.string.set_address_title;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(List<AddressListResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list != null) {
            adapter.setNewData(list);
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
    }

    @Override
    public void deleteSuccess() {
        mPresenter.getData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
